package com.mms.context;

import com.mms.utils.RequestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by dan3652@sina.com on 2016-11-25 10:43.
 */
public class FrontContextInterceptor extends HandlerInterceptorAdapter {
    private static final Logger logger = LoggerFactory.getLogger(FrontContextInterceptor.class);

    private List uncheckUrls;
    /**
     * 在controller后拦截   afterCompletion
     */

    /**
     * 在controller前拦截
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /**
         * 处理不拦截
         */
        String requestUri = RequestUtils.getURI(request);
        if(uncheckUrls.contains(requestUri)){
            return true;
        }
        /**
         * 获取URL
         */
        String path = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
        request.setAttribute(Constants.SYSTEM_URL,path);
        if ( 80 == request.getServerPort()){
            request.setAttribute(Constants.SYSTEM_URL,request.getScheme()+"://"+request.getServerName()+request.getContextPath()+"/");
        }
        System.out.println("处理拦截===========================");
        return true;
    }

    public void setUncheckUrls(List uncheckUrls) {
        this.uncheckUrls = uncheckUrls;
    }

    public List getUncheckUrls() {
        return uncheckUrls;
    }
}
